<template>
  <div class="register-view">
    <div class="register-header">
      <h2>创建账号</h2>
      <p>注册一个新的待办事项管理系统账号</p>
    </div>
    
    <el-form 
      ref="registerForm" 
      :model="registerData" 
      :rules="rules" 
      label-position="top"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="registerData.username" placeholder="请输入用户名" />
      </el-form-item>
      
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="registerData.email" placeholder="请输入邮箱" />
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="registerData.password" 
          type="password" 
          placeholder="请输入密码" 
          show-password
        />
      </el-form-item>
      
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input 
          v-model="registerData.confirmPassword" 
          type="password" 
          placeholder="请再次输入密码" 
          show-password
        />
      </el-form-item>
      
      <el-form-item>
        <el-button 
          type="primary" 
          :loading="isLoading" 
          @click="handleRegister" 
          class="submit-btn"
        >
          注册
        </el-button>
      </el-form-item>
    </el-form>
    
    <div class="auth-links">
      <router-link to="/auth/login">已有账号？立即登录</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()
const registerForm = ref(null)
const isLoading = ref(false)

const registerData = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerData.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerForm.value) return
  
  await registerForm.value.validate(async (valid) => {
    if (valid) {
      isLoading.value = true
      try {
        const { confirmPassword, ...userData } = registerData
        const success = await userStore.register(userData)
        if (success) {
          ElMessage.success('注册成功')
          await router.push('/dashboard')
        }
        if (!success) {
          ElMessage.error('注册失败，请稍后再试')
        }
      } catch (error) {
        ElMessage.error('注册时发生错误')
        console.error(error)
      } finally {
        isLoading.value = false
      }
    }
  })
}
</script>

<style scoped>
.register-view {
  width: 100%;
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-header h2 {
  font-size: 1.8rem;
  color: #303133;
  margin-bottom: 0.5rem;
}

.register-header p {
  color: #606266;
  font-size: 1rem;
}

.submit-btn {
  width: 100%;
  margin-top: 1rem;
  height: 40px;
  font-size: 16px;
}

.auth-links {
  margin-top: 1.5rem;
  text-align: center;
}

.auth-links a {
  color: #409eff;
  text-decoration: none;
  font-size: 0.9rem;
}

.auth-links a:hover {
  text-decoration: underline;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset !important;
}
</style>